package com.example.baseadaptertest.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.baseadaptertest.R;
import com.example.baseadaptertest.bean.Planet;

import java.util.List;

public class PlantBaseAdapter extends BaseAdapter implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private Context mContext;
    private List<Planet> list;

    private int layout;

    public PlantBaseAdapter(Context mContext, List<Planet> list, int layout) {
        this.mContext = mContext;
        this.list = list;
        this.layout = layout;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return list.get(i).getId();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null){
            view = LayoutInflater.from(mContext).inflate(layout, viewGroup, false);
            ImageView img = view.findViewById(R.id.img);
            TextView name = view.findViewById(R.id.name);
            TextView desc = view.findViewById(R.id.desc);
            holder = new ViewHolder(img, name, desc);
            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }

        Planet planet = list.get(i);
        holder.img.setImageResource(planet.getImage());
        holder.name.setText(planet.getName());
        holder.desc.setText(planet.getDesc());
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Planet planet = list.get(i);
        String msg = "当前点击的行星是:" + planet.getName() + " 点击的position是" + i + ",点击的l是" + l;
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        return false;
    }

    class ViewHolder {
         ImageView img;
         TextView name;
         TextView desc;

        public ViewHolder(ImageView img, TextView name, TextView desc) {
            this.img = img;
            this.name = name;
            this.desc = desc;
        }
    }


}
